package com.tucc.hello.leetcode.no1_400.no201_400.no374;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution extends GuessGame {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int n = 2126753390;
        int pick = 1702766719;
        solution.setRight(pick);
        System.out.println(solution.guessNumber(n));
    }

    public int guessNumber(int n) {
        long left = 1, right = Integer.MAX_VALUE;
        int guess = guess(n);
        if (guess == 0) {
            return n;
        } else if (guess == 1) {
            left = n + 1;
        } else {
            right = n - 1;
        }
        while (left < right) {
            long middle = (left + right) / 2;
            int guessResult = guess((int)middle);
            if (guessResult == 0) {
                return (int)middle;
            } else if (guessResult == 1) {
                left = middle + 1;
            } else {
                right = middle - 1;
            }
        }
        if (guess((int)left) == 0) {
            return (int)left;
        }
        return (int)right;
    }
}

class GuessGame {

    private int right;

    public void setRight(int num) {
        right = num;
    }

    public int guess(int num) {
        if (num == right) {
            return 0;
        }
        if (num < right) {
            return 1;
        }
        return -1;
    }
}